package com.google.code.jerseyclients.httpclientfour;

/*
 * Olivier Lamy
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.ws.rs.core.MultivaluedMap;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.time.StopWatch;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpHead;
import org.apache.http.client.methods.HttpOptions;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.params.HttpConnectionParams;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.code.jerseyclients.JerseyHttpClientConfig;
import com.sun.jersey.api.client.ClientHandlerException;
import com.sun.jersey.api.client.ClientRequest;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.ClientResponse.Status;
import com.sun.jersey.api.client.CommittingOutputStream;
import com.sun.jersey.api.client.TerminatingClientHandler;
import com.sun.jersey.core.header.InBoundHeaders;

/**
 * @author <a href="mailto:olamy@apache.org">Olivier Lamy</a>
 * @since
 * @version $Id: ApacheHttpClientFourHandler.java 9 2010-11-04 18:55:54Z oliver.lamy@gmail.com $
 */
public class ApacheHttpClientFourHandler
    extends TerminatingClientHandler
{
    private Logger log = LoggerFactory.getLogger( getClass() );

    private final HttpClient client;

    private JerseyHttpClientConfig jerseyHttpClientConfig;

    /**
     * Create a new root handler with an {@link HttpClient}.
     * 
     * @param client the {@link HttpClient}.
     */
    public ApacheHttpClientFourHandler( HttpClient client, JerseyHttpClientConfig config )
    {
        this.client = client;
        this.jerseyHttpClientConfig = config;
    }

    /**
     * Get the {@link HttpClient}.
     * 
     * @return the {@link HttpClient}.
     */
    public HttpClient getHttpClient()
    {
        return client;
    }

    public ClientResponse handle( final ClientRequest clientRequest )
        throws ClientHandlerException
    {

        if ( this.jerseyHttpClientConfig.getApplicationCode() != null )
        {
            clientRequest.getHeaders().add( this.jerseyHttpClientConfig.getApplicationCodeHeader(),
                                            this.jerseyHttpClientConfig.getApplicationCode() );
        }
        if ( this.jerseyHttpClientConfig.getOptionnalHeaders() != null )
        {
            for ( Entry<String, String> entry : this.jerseyHttpClientConfig.getOptionnalHeaders().entrySet() )
            {
                clientRequest.getHeaders().add( entry.getKey(), entry.getValue() );
            }
        }
        // final Map<String, Object> props = cr.getProperties();

        final HttpRequestBase method = getHttpMethod( clientRequest );
        
        // Set the read timeout
        final Integer readTimeout = jerseyHttpClientConfig.getReadTimeOut();
        if ( readTimeout != null )
        {
            HttpConnectionParams.setSoTimeout( method.getParams(), readTimeout.intValue() );
        }

        // FIXME penser au header http
        // DEBUG|wire.header|>> "Cache-Control: no-cache[\r][\n]"
        // DEBUG|wire.header|>> "Pragma: no-cache[\r][\n]"
        if ( method instanceof HttpEntityEnclosingRequestBase )
        {
            final HttpEntityEnclosingRequestBase entMethod = (HttpEntityEnclosingRequestBase) method;

            if ( clientRequest.getEntity() != null )
            {
                final RequestEntityWriter re = getRequestEntityWriter( clientRequest );

                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                try
                {
                    re.writeRequestEntity( new CommittingOutputStream( baos )
                    {
                        @Override
                        protected void commit()
                            throws IOException
                        {
                            writeOutBoundHeaders( clientRequest.getHeaders(), method );
                        }
                    } );
                }
                catch ( IOException ex )
                {
                    throw new ClientHandlerException( ex );
                }

                final byte[] content = baos.toByteArray();
                HttpEntity httpEntity = new ByteArrayEntity( content );
                entMethod.setEntity( httpEntity );

            }
        }
        else
        {
            writeOutBoundHeaders( clientRequest.getHeaders(), method );
        }

        try
        {
            StopWatch stopWatch = new StopWatch();
            stopWatch.reset();
            stopWatch.start();
            HttpResponse httpResponse = client.execute( method );
            int httpReturnCode = httpResponse.getStatusLine().getStatusCode();
            stopWatch.stop();
            log.info( "time to call rest url " + clientRequest.getURI() + ", " + stopWatch.getTime() + " ms" );

            if ( httpReturnCode == Status.NO_CONTENT.getStatusCode() )
            {
                return new ClientResponse( httpReturnCode, getInBoundHeaders( httpResponse ),
                                           IOUtils.toInputStream( "" ), getMessageBodyWorkers() );
            }

            return new ClientResponse( httpReturnCode, getInBoundHeaders( httpResponse ),
                                       httpResponse.getEntity() == null ? IOUtils.toInputStream( "" ) : httpResponse
                                           .getEntity().getContent(), getMessageBodyWorkers() );
        }catch ( Exception e )
        {
            throw new ClientHandlerException( e );
        }
    }

    private HttpRequestBase getHttpMethod( ClientRequest cr )
    {
        final String strMethod = cr.getMethod();
        final String uri = cr.getURI().toString();

        if ( strMethod.equals( "GET" ) )
        {
            return new HttpGet( uri );
        }
        else if ( strMethod.equals( "POST" ) )
        {
            return new HttpPost( uri );
        }
        else if ( strMethod.equals( "PUT" ) )
        {
            return new HttpPut( uri );
        }
        else if ( strMethod.equals( "DELETE" ) )
        {
            return new HttpDelete( uri );
        }
        else if ( strMethod.equals( "HEAD" ) )
        {
            return new HttpHead( uri );
        }
        else if ( strMethod.equals( "OPTIONS" ) )
        {
            return new HttpOptions( uri );
        }
        else
        {
            throw new ClientHandlerException( "Method " + strMethod + " is not supported." );
        }
    }

    private void writeOutBoundHeaders( MultivaluedMap<String, Object> metadata, HttpRequestBase method )
    {
        for ( Map.Entry<String, List<Object>> e : metadata.entrySet() )
        {
            List<Object> vs = e.getValue();
            if ( vs.size() == 1 )
            {
                method.addHeader( e.getKey(), headerValueToString( vs.get( 0 ) ) );
            }
            else
            {
                StringBuilder b = new StringBuilder();
                for ( Object v : e.getValue() )
                {
                    if ( b.length() > 0 )
                    {
                        b.append( ',' );
                    }
                    b.append( headerValueToString( v ) );
                }
                method.addHeader( e.getKey(), b.toString() );
            }
        }
    }

    /**
     * @param httpResponse
     * @return
     */
    private InBoundHeaders getInBoundHeaders( HttpResponse httpResponse )
    {
        InBoundHeaders headers = new InBoundHeaders();
        Header[] respHeaders = httpResponse.getAllHeaders();
        for ( Header header : respHeaders )
        {
            List<String> list = headers.get( header.getName() );
            if ( list == null )
            {
                list = new ArrayList<String>();
            }
            list.add( header.getValue() );
            headers.put( header.getName(), list );
        }
        return headers;
    }

}
